package com.cat.slidingWindow;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/permutation-in-string/description/
 * @create 2025/9/16 11:19
 * @since JDK17
 */

public class Solution31 {
    static int[] h1 = new int[128], h2 = new int[128];

    public boolean checkInclusion(String s1, String s2) {
        Arrays.fill(h1, 0);
        Arrays.fill(h2, 0);
        int n = s1.length(), m = s2.length(), debt = n;
        for (int i = 0; i < n; i++) {   // 将s1都记录起来
            h1[s1.charAt(i) - 'a']++;
        }
        for (int l = 0, r = 0; r < m; ) {
            while (r - l < n) { // 最多n个
                debt -= ++h2[s2.charAt(r)] == h1[s2.charAt(r)] && h1[s2.charAt(r)] != 0 ? 1 : 0;
                r++;
            }
            if (debt == 0) {
                return true;
            } else {
                debt -= h2[s2.charAt(l)]-- == h1[s2.charAt(l)] && h1[s2.charAt(r)] != 0 ? 1 : 0;
                l++;
            }
        }

        return false;
    }
}
